package CSDN;

/**
 * Created with IntelliJ IDEA.
 * Description: N个线程，M把锁
 * User: 王圆豪
 * Date: 2025-05-07
 * Time: 0:04
 */
public class Test12 {
    public static void main(String[] args) {
        Object locker1 = new Object();
        Object locker2 = new Object();
        Thread t1 = new Thread(()->{
            synchronized (locker1){
                try {
                    System.out.println("t1获取了locker1！");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (locker2){
                    System.out.println("t1获取了两把锁！");
                }
            }
        });
        Thread t2 = new Thread(()->{
            synchronized (locker2){
                try {
                    System.out.println("t2获取了locker2！");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (locker1){
                    System.out.println("t2获取了两把锁！");
                }
            }
        });
        t1.start();
        t2.start();

    }
}
